package com.lrkj.payfee.controller;

import com.lrkj.payfee.service.IReceiptService;
import com.lrkj.payfee.vo.ReceiptVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.payfee.domain.HandRecord;
import com.lrkj.payfee.service.IHandRecordService;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.common.core.page.TableDataInfo;

/**
 * 交班管理Controller
 *
 * @author yajun_ren
 * @date 2022-03-30
 */
@RestController
@RequestMapping("/payfee/HandRecord")
@Api(tags = "交班管理")
public class HandRecordController extends BaseController
{
    @Autowired
    private IHandRecordService handRecordService;

    @Autowired
    private IReceiptService iReceiptService;

    /**
     * 查询交班管理列表
     */
    @ApiOperation(value = "查询交班管理列表", notes = "查询交班管理列表")
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(HandRecord handRecord)
    {
        startPage();
        this.changeParameter(handRecord.getParams());
        List<HandRecord> list = handRecordService.selectHandRecordList(handRecord);
        return getDataTable(list);
    }

    /**
     * 导出交班管理列表
     */
	@ApiOperation(value = "导出交班管理列表", notes = "导出交班管理列表")
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:export')")
    @Log(title = "交班管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HandRecord handRecord)
    {
        List<HandRecord> list = handRecordService.selectHandRecordList(handRecord);
        ExcelUtil<HandRecord> util = new ExcelUtil<HandRecord>(HandRecord.class);
        return util.exportExcel(list, "交班管理数据");
    }

    /**
     * 获取交班管理详细信息
     */
	@ApiOperation(value = "获取交班管理详细信息", notes = "获取交班管理详细信息")
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(handRecordService.selectHandRecordById(id));
    }

    /**
     * 新增交班管理
     */
    @ApiOperation(value = "新增交班管理", notes = "新增交班管理")
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:add')")
    @Log(title = "交班管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HandRecord handRecord)
    {
        return toAjax(handRecordService.insertHandRecord(handRecord));
    }

    /**
     * 修改交班管理
     */
    @ApiOperation(value = "修改交班管理", notes = "修改交班管理")
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:edit')")
    @Log(title = "交班管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HandRecord handRecord)
    {
        return toAjax(handRecordService.updateHandRecord(handRecord));
    }

    /**
     * 删除交班管理
     */
    @ApiOperation(value = "删除交班管理", notes = "删除交班管理")
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:remove')")
    @Log(title = "交班管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(handRecordService.deleteHandRecordByIds(ids));
    }


    /**
     * 统计当日交班情况
     * @param handRecord
     * @return
     */
    @GetMapping("/onDayReceipt")
    public AjaxResult onDayReceipt(HandRecord handRecord){
        this.changeParameter(handRecord.getParams());
        List<HandRecord> receipts = handRecordService.onDayReceipt(handRecord);
        Map<String,Object> map = new HashMap<>();
        if (receipts.size()>0){
            map.put("sum", receipts.get(0).getDaySum());
            map.put("minDay", receipts.get(0).getMinDay());
            map.put("maxDay", receipts.get(0).getMaxDay());
        }
        return  AjaxResult.success(map);
    }

    /**
     * 统计当日付款情况
     * @param handRecord
     * @return
     */
    @GetMapping("/onDayAdvanceReceipt")
    public AjaxResult onDayAdvanceReceipt(HandRecord handRecord){
        this.changeParameter(handRecord.getParams());
        List<HandRecord> handRecords = handRecordService.onDayAdvanceReceipt(handRecord);
        Map<String,Object> map = new HashMap<>();
        if (handRecords.size()>0){
                map.put("daySum", handRecords.get(0).getDaySum());
                map.put("minDay", handRecords.get(0).getMinDay());
                map.put("maxDay", handRecords.get(0).getMaxDay());
        }
        return  AjaxResult.success(map);
    }


    /**
     * 审核
     * @param handRecord
     * @return
     */
    @PreAuthorize("@ss.hasPermi('payfee:HandRecord:approve')")
    @PutMapping("/approve")
    public AjaxResult approve(@RequestBody HandRecord handRecord){
        return toAjax( handRecordService.approve(handRecord));
    }
}
